import { defineStore } from 'pinia';
import { useStorage } from '@vueuse/core'; // 使用 vueuse 的 useStorage 来持久化状态

// 定义 Pinia Store
export const useCourseStore = defineStore('courseStore', () => {
  // 使用 useStorage 将状态持久化到 localStorage
  const courseid = useStorage<string | null>('course-courseid', null); // 持久化的 courseid
  const coursename = useStorage<string | null>('course-coursename', null); // 持久化的 coursename
  const chaptername = useStorage<string | null>('course-chaptername', null); // 持久化的 chaptername
  const subchaptername = useStorage<string | null>('course-subchaptername', null); // 持久化的 subchaptername

  // Actions
  const setCourseid = (newCourseid: string | null) => {
    courseid.value = newCourseid;
  };

  const setCoursename = (newCoursename: string | null) => {
    coursename.value = newCoursename;
  };

  const setChaptername = (newChaptername: string | null) => {
    chaptername.value = newChaptername;
  };

  const setSubchaptername = (newSubchaptername: string | null) => {
    subchaptername.value = newSubchaptername;
  };

  const clearCourseStore = () => {
    courseid.value = null;
    coursename.value = null;
    chaptername.value = null;
    subchaptername.value = null;
  };

  // 返回状态和方法
  return {
    courseid,
    coursename,
    chaptername,
    subchaptername,
    setCourseid,
    setCoursename,
    setChaptername,
    setSubchaptername,
    clearCourseStore,
  };
});
